package com.iring.controller;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iring.domain.common.User;
import com.iring.vo.common.BooleanResultVO;
import com.iring.vo.sms.SMSSendVO;
import com.iring.vo.user.UserInfoVO;

/**
 * 找回密码controller
 *
 * @author wanggan
 */
@RestController
@RequestMapping("/reg-user")
public class RegsiterController extends BaseController {

    /**
     * 检验用户名是否唯一
     *
     * @param username
     *            用户名
     * @return 如果用户名不存在, 则返回true, 存在返回false
     */
    @RequestMapping(value = "/is-username-available", method = RequestMethod.GET)
    public BooleanResultVO isUsernameAvailable(@RequestParam("username") String username) {
        return new BooleanResultVO(userService.checkUsername(username));
    }

    /**
     * 验证电话并发送验证码
     *
     * @param smsSendVO
     *            {@link SMSSendVO}
     * @return 发送验证码结果
     */
    @RequestMapping(value = "/send-vaild-code", method = RequestMethod.POST)
    public BooleanResultVO sendValidCode(@RequestBody SMSSendVO smsSendVO) {
        checkSessionCodePhoneToken();
        Boolean result = userService.sendCode(smsSendVO);
        updateSessionCodePhoneTimeStamp();
        return new BooleanResultVO(result);
    }

    /**
     * 用户注册
     *
     * @param userInfoVO
     *            {@link UserInfoVO}
     * @return 注册结果
     */
    @RequestMapping(value = "/add-user", method = RequestMethod.POST)
    public BooleanResultVO addUser(@RequestBody UserInfoVO userInfoVO) {
        checkAccessToken();
        User user= userService.addUser(userInfoVO);
        if(null == user){
            return new BooleanResultVO(Boolean.FALSE);
        }
        logService.saveLog(user.getUsername(),"User registration",getUserRealIp(),true);
        removeSessionCodePhoneTimeStamp();
        return new BooleanResultVO(Boolean.TRUE);
    }

}
